package com.egreen.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egreen.common.pojo.entity.user.*;
import com.egreen.common.pojo.DTO.user.userLocationDTO.UserLocationUpdateDTO;
import com.egreen.user.dao.UserLocationDao;
import com.egreen.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cloudJiang
 * @since 2022-07-16
 */
@Service
public class UserLocationServiceImpl extends ServiceImpl<UserLocationDao, UserLocation> implements IUserLocationService {
    @Autowired
    IUserService userService;

    @Autowired
    ICountryService countryService;

    @Autowired
    IUserLocationService userLocationService;

    @Autowired
    IProvinceService provinceService;

    @Autowired
    ILocationDetailService locationDetailService;

    @Autowired
    IHistoryLocationDetailService historyLocationDetailService;

    @Autowired
    IUserHistoryLocationService userHistoryLocationService;

    @Override
    @Transactional(rollbackFor = SQLException.class)
    public void userLocationUpdate(UserLocationUpdateDTO userLocationUpdateDTO) {
        QueryWrapper<User> userwrapper = new QueryWrapper<>();
        userwrapper.eq("id", userLocationUpdateDTO.getUserId());
        User user = userService.getOne(userwrapper);

        //获取用户原来的地址并存储到数据库
        QueryWrapper<UserLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("id", user.getLocationId());
        UserLocation userLocation = userLocationService.getOne(locationQueryWrapper);

        QueryWrapper<LocationDetail> locationDetailWrapper = new QueryWrapper<>();
        locationDetailWrapper.eq("id", userLocation.getDetailId());
        LocationDetail locationDetail = locationDetailService.getOne(locationDetailWrapper);


        //将原地址信息备份至数据库
        HistoryLocationDetail historyLocationDetail = new HistoryLocationDetail();
        historyLocationDetail.setDistrict(locationDetail.getDistrict());
        historyLocationDetail.setCommunity(locationDetail.getCommunity());
        historyLocationDetail.setLocationDetails(locationDetail.getLocationDetails());
        historyLocationDetailService.save(historyLocationDetail);

        UserHistoryLocation userHistoryLocation = new UserHistoryLocation();
        userHistoryLocation.setUserId(user.getId());
        userHistoryLocation.setCountryId(1);
        userHistoryLocation.setProvinceId(userLocation.getProvinceId());
        userHistoryLocation.setCityName(userLocation.getCityName());
        userHistoryLocation.setDetailId(historyLocationDetail.getId());
        userHistoryLocationService.save(userHistoryLocation);

        //进行地址更新
        QueryWrapper<Province> wrapper = new QueryWrapper();
        wrapper.eq("name", userLocationUpdateDTO.getProvince());
        userLocation.setCountryId(1);
        userLocation.setProvinceId(provinceService.getOne(wrapper).getId());
        userLocation.setCityName(userLocationUpdateDTO.getCityName());

        locationDetail.setDistrict(userLocationUpdateDTO.getDistrict());
        locationDetail.setCommunity(userLocationUpdateDTO.getCommunity());
        locationDetail.setLocationDetails(userLocationUpdateDTO.getLocationDetails());


        locationDetailService.updateById(locationDetail);
        userLocationService.updateById(userLocation);
    }
}
